#include "../common/icsearch.hpp"
#include <gflags/gflags.h>
#include <memory>
DEFINE_bool(run_mode, false, "程序运行模式:true为发布模式,false为调试模式");
DEFINE_string(log_file, "", "发布模式中,用于存放日志的文件路径");
DEFINE_int32(log_level, 0, "发布模式中,日志输出等级");
int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    auto client = std::make_shared<elasticlient::Client> (std::vector<std::string>{"http://127.0.0.1:9200/"});//注意末尾的/要加
    //std::shared_ptr<elasticlient::Client> client(new elasticlient::Client({"http://127.0.0.1:9200/"}));
    bool ret = ESIndex(client,"test_user").append("nickname").append("phone","keyword","standard",true).create();
    if (ret)
    {
        LOG_INFO("创建索引成功");
    }
//数据新增
    ret = ESInsert(client, "test_user").append("nickname", "张三").append("phone", "123456789").insert("00001");
    if(!ret)
    {
        LOG_ERROR("插入数据失败");  
        return -1;
    }
//数据修改
    ret = ESInsert(client, "test_user").append("nickname", "张三").append("phone", "987654321").insert("00001");
    if(!ret)
    {
        LOG_ERROR("修改数据失败");  
        return -1;
    }
    Json::Value user = ESSearch(client, "test_user").append_should_match("phone.keyword", "987654321").append_must_not_terms("nickname.keyword",{"张三"}).search();//keyword表示这是一个关键字，不会进行分词
    if(user.empty()||user.isArray()==false)
    {
        LOG_ERROR("查询数据失败");
        return -1;
    }
    else
    {
       int sz = user.size();
         for(int i=0;i<sz;i++)
         {
              std::cout<<user[i]["_source"]["nickname"].asString()<<std::endl;
         }
    }
//数据删除
    ret = ESRemove(client, "test_user").remove("00001");
    return 0;
}